﻿Ext.define('HIT.view.ReportViewPanel', {
	extend : 'Ext.form.Panel',
	alias : 'widget.reportcheck',

	frame : false,
	id : 'report-view-grid',
	title : Share.T('报告查看'),
	layout:'fit',

	initComponent : function() {
		var me = this;
		var json_f = '';
		var columns = '';
		var totalcount = 0;
		var page_start = 1;
		var page_limit = 30;
		var reportStore = '';
		var report_hid = Ext.getCmp('tab-report-report').getSelectionModel().getSelection()[0].get('report_hid');
		// store对象在创建时需要制定fields属性，因此使用ajax首先从后台获得列名
		Ext.Ajax.request({
			// 从controller返回的response中读取数据
			url : 'getreportcheck',
			method : 'POST',
			params : {
				report_hid : report_hid,
				page_start : page_start,
				page_limit : page_limit
			},
			async : false,
			success : function(response) {
				s = response.responseText;
				
				json_f = Ext.JSON.decode(s);
				nowcount = json_f.totalRecord;
				totalcount = json_f.count;
				columns = json_f.columns;
				
				reportStore = Ext.create('Ext.data.Store', {
					proxy : {
						type : 'ajax',
						actionMethods : 'POST',
						url : 'getreportcheck',
						reader : {
							type : 'json',
							root : 'data'
						}
					},
					 fields : json_f.fields,
					 data : json_f.data
				});
			}
		});
		
		Ext.applyIf(me, {
			items :[ {
				xtype : 'grid',
				id : 'report_view',
				// 表头数据，至关重要
				columns : columns,
				render : this.body,
				store : reportStore,
				anchor: '100%',
				frame : false
			} ],
			dockedItems : [ {
                xtype : 'toolbar',
                dock : 'bottom',
                items : [ {
					xtype : 'button',
					id : 'page_first',
					width : 23,
					height : 22,
					iconCls : 'x-btn-icon x-tbar-page-first',
					listeners : {
						click : function(){
							var page_start = 1;
							page_limit = Ext.getCmp('page_limit').getValue();
							var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
							Ext.getCmp('now_page').setValue(1);
							Ext.getCmp('now_data').setText('1 - ' + page_limit);
							Ext.getCmp('page_next').setDisabled(false);
							Ext.getCmp('page_last').setDisabled(false);
							Ext.getCmp('page_first').setDisabled(true);
							Ext.getCmp('page_prev').setDisabled(true);
						}
					}
				}, {
					xtype : 'button',
					id : 'page_prev',
					width : 23,
					height : 22,
					iconCls : 'x-btn-icon x-tbar-page-prev',
					listeners : {
						click : function(){
							var nowpage = Ext.getCmp('now_page').getValue();
							var page_limit = Ext.getCmp('page_limit').getValue();
							var page_start = (nowpage - 1) * page_limit - page_limit + 1;
							
							var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
							Ext.getCmp('now_page').setValue(nowpage - 1);
							Ext.getCmp('now_data').setText(nowpage - 1);
							Ext.getCmp('page_next').setDisabled(false);
							Ext.getCmp('page_last').setDisabled(false);
							
							if (Ext.getCmp('now_page').getValue() == 1) {
								Ext.getCmp('now_data').setText('1 - ' + page_limit);
								Ext.getCmp('page_first').setDisabled(true);
								Ext.getCmp('page_prev').setDisabled(true);
							} else {
								Ext.getCmp('now_data').setText(((nowpage - 2) * page_limit + 1) + ' - ' + ((nowpage - 1) * page_limit));
							}
						}
					}
				}, {
					xtype: 'tbseparator'
				}, {
					xtype : 'numberfield',
					hideTrigger : true,
					id : 'now_page',
					width : 70,
					minValue : 1,
					allowNegative : false,
					allowDecimals : false,
					value : 1,
					labelWidth : 20,
					labelSeparator : '',
					fieldLabel : '第',
					listeners : {
						specialkey : function(field, e) {
							if (e.getKey() == Ext.EventObject.ENTER) {
								var nowpage = Ext.getCmp('now_page').getValue();
								var page_limit = Ext.getCmp('page_limit').getValue();
								var page_start = (nowpage - 1) * page_limit + 1;
								if (nowpage > Math.ceil(totalcount / page_limit) || nowpage == 0 || nowpage == 1 || nowpage == '' || nowpage == null) {
									page_start = 1;
									Ext.getCmp('now_page').setValue(1);
									Ext.getCmp('now_data').setText('1 - ' + page_limit);
									Ext.getCmp('page_next').setDisabled(false);
									Ext.getCmp('page_last').setDisabled(false);
									Ext.getCmp('page_first').setDisabled(true);
									Ext.getCmp('page_prev').setDisabled(true);
								} else {
									Ext.getCmp('now_data').setText((((nowpage - 1) * page_limit) + 1) + ' - ' + nowpage * page_limit);
									Ext.getCmp('page_next').setDisabled(false);
									Ext.getCmp('page_last').setDisabled(false);
									Ext.getCmp('page_first').setDisabled(false);
									Ext.getCmp('page_prev').setDisabled(false);
								}
								var store = Ext.getCmp('report_view').getStore();
								store.load({
									params : {
										page_start : page_start,
										page_limit : page_limit,
										report_hid : report_hid
									}
								});
							}
						}
					}
				}, {
					xtype : 'tbspacer',
					width : 5
				}, {
					xtype : 'label',
					width : 30,
					text : '页,共'
				}, {
					xtype : 'label',
					id : 'total_page',
					text : ''
				}, {
					xtype : 'label',
					width : 12,
					text : '页'
				}, {
					xtype: 'tbseparator'
				}, {
					xtype : 'button',
					id : 'page_next',
					width : 23,
					height : 22,
					iconCls : 'x-btn-icon x-tbar-page-next',
					listeners : {
						click : function(){
							var nowpage = Ext.getCmp('now_page').getValue();
							var page_limit = Ext.getCmp('page_limit').getValue();
							var page_start = nowpage * page_limit + 1;
							
							var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
							Ext.getCmp('now_page').setValue(nowpage + 1);
							Ext.getCmp('now_data').setText(nowpage + 1);
							Ext.getCmp('page_first').setDisabled(false);
							Ext.getCmp('page_prev').setDisabled(false);
							
							if ((nowpage + 1) == Math.ceil(totalcount / page_limit)) {
								Ext.getCmp('page_next').setDisabled(true);
								Ext.getCmp('page_last').setDisabled(true);
								Ext.getCmp('now_data').setText((nowpage * page_limit + 1) + ' - ' + totalcount);
							} else {
								Ext.getCmp('now_data').setText((nowpage * page_limit + 1) + ' - ' + (Ext.getCmp('now_page').getValue() * page_limit));
							}
						}
					}
				}, {
					xtype : 'button',
					id : 'page_last',
					width : 23,
					height : 22,
					iconCls : 'x-btn-icon x-tbar-page-last',
					listeners : {
						click : function(){
							var page_limit = Ext.getCmp('page_limit').getValue();
							var page_start = page_limit * (Math.ceil(totalcount / page_limit) - 1) + 1;
							
							var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
							Ext.getCmp('now_page').setValue(Math.ceil(totalcount / page_limit));
							Ext.getCmp('now_data').setText((page_limit * (Math.ceil(totalcount / page_limit) - 1) + 1) + ' - ' + totalcount);
							Ext.getCmp('page_next').setDisabled(true);
							Ext.getCmp('page_last').setDisabled(true);
							Ext.getCmp('page_first').setDisabled(false);
							Ext.getCmp('page_prev').setDisabled(false);
						}
					}
				}, {
					xtype: 'tbseparator'
				}, {
					xtype : 'button',
					id : 'refresh',
					width : 23,
					height : 22,
					iconCls : 'x-btn-icon x-tbar-loading',
					listeners : {
						click : function () {
							var now_page = Ext.getCmp('now_page').getValue();
							var page_limit = Ext.getCmp('page_limit').getValue();
							
							if (now_page == 0 || now_page == '' || now_page == null || (now_page > Math.ceil(totalcount / page_limit))) {
								Ext.getCmp('now_page').setValue('1');
								now_page = 1;
							}
							if (page_limit == 0 || page_limit == '' || page_limit == null || page_limit > 500) {
								Ext.getCmp('page_limit').setValue(30);
								page_limit = 30;
							}
							
							var page_start = (now_page - 1) * page_limit + 1;
							var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
						}
					}
				}, '->', {
					xtype : 'label',
					text : '显示'
				}, {
					xtype : 'label',
					id : 'now_data',
					text : '1 - 30'
				}, {
					xtype : 'label',
					text : '条, 共'
				}, {
					xtype : 'label',
					id : 'total_count',
					text : totalcount
				}, {
					xtype : 'label',
					text : '条'
				}, {
					xtype : 'tbspacer',
					width : 3
				}, {
					xtype: 'tbseparator',
					width : 10
				}, {
					xtype : 'combo',
					id : 'page_limit',
					width : 50,
					value : 30,
					store: new Ext.data.Store({
			    		fields:['text','value'],
			    		data:[{text:10,value:10},
			    		      {text:15,value:15},   					    		     
			    		      {text:20,value:20},
			    		      {text:25,value:25},
			    		      {text:30,value:30},
			    		      {text:50,value:50},
			    		      {text:100,value:100},
			    		      {text:200,value:200},
			    		      {text:500,value:500}
			    		]
				    }),
				    displayField: 'text',
				    valueField: 'value',
				    listeners : {
				    	select : function(){
				    		var page_start = 1;
				    		var page_limit = this.value;
				    		
				    		var store = Ext.getCmp('report_view').getStore();
			          		store.load({
			          			params : {
			          				page_start : page_start,
			          				page_limit : page_limit,
			          				report_hid : report_hid
			          			}
		          			});
			          		
			          		if (totalcount == 0){
								Ext.getCmp('page_next').setDisabled(true);
								Ext.getCmp('page_last').setDisabled(true);
								Ext.getCmp('now_page').setValue(0);
								Ext.getCmp('now_page').setDisabled(true);
								Ext.getCmp('now_data').setText('0');
								Ext.getCmp('total_page').setText('0');
								Ext.getCmp('total_count').setText('0');
				    		} else if (totalcount > 0 && totalcount > page_limit) {
				    			var total_page = Math.ceil(totalcount / page_limit);
					    		Ext.getCmp('total_page').setText(total_page);
					    		Ext.getCmp('now_page').setValue('1');
					    		Ext.getCmp('now_data').setText('1 - ' + page_limit);
					    		Ext.getCmp('page_next').setDisabled(false);
								Ext.getCmp('page_last').setDisabled(false);
					    		Ext.getCmp('page_first').setDisabled(true);
								Ext.getCmp('page_prev').setDisabled(true);
				    		} else {
				    			Ext.getCmp('now_data').setText('1 - ' + totalcount);
				    			Ext.getCmp('page_next').setDisabled(true);
								Ext.getCmp('page_last').setDisabled(true);
								Ext.getCmp('page_first').setDisabled(true);
								Ext.getCmp('page_prev').setDisabled(true);
				    		}
				    	},
				    	specialkey : function(field, e) {
							if (e.getKey() == Ext.EventObject.ENTER) {
								var page_start = 1;
					    		var page_limit = Ext.getCmp('page_limit').getValue();
								if (page_limit == 0 || page_limit > 500 || page_limit == '' || page_limit == null) {
									page_limit = 30;
									Ext.getCmp('page_limit').setValue(30);
								}
					    		var store = Ext.getCmp('report_view').getStore();
				          		store.load({
				          			params : {
				          				page_start : page_start,
				          				page_limit : page_limit,
				          				report_hid : report_hid
				          			}
			          			});
				          		
				          		if (totalcount == 0){
									Ext.getCmp('page_next').setDisabled(true);
									Ext.getCmp('page_last').setDisabled(true);
									Ext.getCmp('now_page').setValue(0);
									Ext.getCmp('now_page').setDisabled(true);
									Ext.getCmp('now_data').setText('0');
									Ext.getCmp('total_page').setText('0');
									Ext.getCmp('total_count').setText('0');
					    		} else if (totalcount > 0 && totalcount > page_limit) {
					    			var total_page = Math.ceil(totalcount / page_limit);
						    		Ext.getCmp('total_page').setText(total_page);
						    		Ext.getCmp('now_page').setValue('1');
						    		Ext.getCmp('now_data').setText('1 - ' + page_limit);
						    		Ext.getCmp('page_next').setDisabled(false);
									Ext.getCmp('page_last').setDisabled(false);
						    		Ext.getCmp('page_first').setDisabled(true);
									Ext.getCmp('page_prev').setDisabled(true);
					    		} else {
					    			Ext.getCmp('now_data').setText('1 - ' + totalcount);
					    			Ext.getCmp('page_next').setDisabled(true);
									Ext.getCmp('page_last').setDisabled(true);
									Ext.getCmp('page_first').setDisabled(true);
									Ext.getCmp('page_prev').setDisabled(true);
					    		}
							}
						}
				    }
				}, {
					xtype : 'tbspacer',
					width : 10
				} ]
			},{
				xtype : 'textfield',
				width : 20,
				hidden : true,
				id : 'hidden_report_hid'
			} ]
		});
		me.callParent(arguments);
		
		// 页面初始化分页控件初始值
		Ext.getCmp('page_first').setDisabled(true);
		Ext.getCmp('page_prev').setDisabled(true);
		if (totalcount == 0){
			Ext.getCmp('actionbar').removeAll();
            
			var actionbar = Ext.getCmp('actionbar');
			actionbar.items.each(function(item) {
		        item.items.each(function(subItem) {
		            if (subItem.id == 'report_export') {
		                subItem.setDisabled(true);
		            }
		        });
		    });
			Ext.getCmp('page_next').setDisabled(true);
			Ext.getCmp('page_last').setDisabled(true);
			Ext.getCmp('now_page').setValue(0);
			Ext.getCmp('now_page').setDisabled(true);
			Ext.getCmp('now_data').setText('0');
			Ext.getCmp('total_page').setText('0');
			Ext.getCmp('total_count').setText('0');
		} else if (totalcount < 30){
			Ext.getCmp('page_next').setDisabled(true);
			Ext.getCmp('page_last').setDisabled(true);
			Ext.getCmp('total_page').setText(1);
			Ext.getCmp('now_data').setText(1 + ' - ' + totalcount);
		} else {
			Ext.getCmp('total_page').setText(Math.ceil(totalcount / 30));
		}
	}
});